Problem Set 1: Hodrick-Prescott Filter
¶
Introduction¶
A commonly used approach to decompose a time series into a permanent component \(y^p\) and a transitory (cyclical) component \(y^c\) goes back to Hodrick and Prescott (1997). Suppose you have a sequence of data, \(y_t\) with \(t = 1,...,T\) (i.e. there are \(T\) total observations). Our objective is to find a trend \(\{y^p_t\}^T_{t=1}\) to minimize the following objective function:
where the parameter \(\lambda \geq 0\).
Exercise 1¶
Question: Provide a verbal interpretation of this objective function. What are the trade-offs?*
Answer: The first term of the function is the sum of quadratic differences between the realization \(y_t\) and the trend component \(y^p_t\). If the function would only consist of this component, the minimization problem would yield \(y^p_t = y_t\). Therefore, \(y^p_t\) has also to minimize the squared difference to its neighbours \(y^p_{t+1}\) and \(y^p_{t-1}\) so that all trend components are similar to each other. The weighting parameter \(\lambda\) controls how important it is that the trend values are similar to each other. A higher \(\lambda\) will lead to more equal trend components meaning a smoother trend, but it might also wash out important differences in the trend.
Exercise 2¶
Question: Prove that if \(\lambda = 0\), the solution is \(y^p_t = y_t \forall t\), i.e. the trend and the actual series are identical.
Answer: If \(\lambda = 0\) the former minimization problem simplifies to
The first derivative with respect to \(y^p_t\) is given by
Exercise 3¶
Question: Prove that as \(\lambda \to \infty\), the filtered series \(\{y^p_t\}^T_{t=1}\) is linear (i.e. \(y^p_t = \beta t\) for some \(\beta\))
Answer: If the formula is divided by \(\lambda\) and \(\lambda \to \infty\) the first term of the equation approaches 0.
Exercise 4¶
Question: For the more general case in which \(0 < \lambda < \infty\), derive analytical conditions which implicitly define the trend. To do this, take the derivative with respect to \(y^p_t\) for each \(t = 1, \dots, T\) and set them equal to zero, yielding \(T\) first order conditions.
Answer: Here are the three different derivations:
For \(y^p_1\):
For \(y^p_2\):
For \(y^p_t\) for \(3 \leq t \leq T-2\):
for \(y^p_{T-1}\):
for \(y^p_{T}\):
Exercise 5¶
Question: Write a Julia function to find the trend taking the actual data series \(y_t\) and parameter \(\lambda\) as inputs. To do this, express the first order conditions in Exercise 4 in matrix form as follows:
where \(\Lambda\) is a \(T \times T\) matrix whose elements are functions of \(\lambda\), \(Y^t\) is a \(T \times 1\) vector equal to \([y^t_1, y^t_2, \dots, y^t_T]'\) and \(Y\) is a \(T \times 1\) vector equal to \([y_1, y_2, \dots, y_T]'\).
In [1]:
function hodrick_prescott_filter(y, lambda)
T = size(y, 1)
matrix = zeros(T, T)
matrix[1, 1:3] = [1 + lambda, -2 * lambda, lambda]
matrix[2, 1:4] = [-2 * lambda, 1 + 5 * lambda, -4 * lambda, lambda]
for i = 3 : T - 2
matrix[i, i-2 : i+2] = [lambda, -4*lambda, 1 + 6 * lambda, -4 * lambda, lambda]
end
matrix[T-1, T-3:T] = [lambda, -4 * lambda, 1 + 5 * lambda, -2 * lambda]
matrix[T, T-2:T] = [lambda, -2 * lambda, 1 + lambda]
trend = matrix \ y
cycle = y - trend
return trend, cycle
end;